﻿Shader "RenderingPractice/SingleTexture"//单张纹理采样
{
    Properties
    {
		_Color("Color Tint",Color) = (1,1,1,1)//颜色基调
        _MainTex ("Main Texture", 2D) = "white" {}//主纹理
		_Specular("Specular Color",Color) = (1,1,1,1)//镜面反射颜色
		_Gloss("Gloss(光泽度)",Range(8.0,256.0)) = 8.0//光泽度（表面光滑度，影响高光大小）
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex VertexFunction
            #pragma fragment FragmentFunction

            #include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Color;//颜色基调
			sampler2D _MainTex;//主纹理（漫反射颜色）
            float4 _MainTex_ST;//主纹理 缩放/平移
			fixed4 _Specular;//镜面反射颜色
			float _Gloss;//光泽度（表面光滑度，影响高光大小）

            struct a2v
            {
                float4 vertex : POSITION;
				float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;//第一组纹理的UV
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float4 worldPos : TEXCOORD1;
                float2 uv : TEXCOORD2;//第一组纹理的UV
            };

            v2f VertexFunction (a2v v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld , v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord , _MainTex);//纹理坐标变换
                return o;
            }

            fixed4 FragmentFunction (v2f i) : SV_Target
            {
				//1.纹理采样，计算 albedo：
				fixed3 albedo = tex2D(_MainTex , i.uv).rgb * _Color.rgb;
				//2.环境光：
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				//3.漫反射：
				fixed3 worldNormalDir = normalize(i.worldNormal);//n
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));//l
				fixed3 diffuseColor = _LightColor0.rgb * albedo.rgb * saturate(dot(worldNormalDir , worldLightDir)) * albedo;
				//4.镜面反射：（Blinn-Phong）
				float3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
				float3 hDir = normalize(viewDir + worldLightDir);//h
				fixed3 specularColor = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormalDir , hDir)) , _Gloss);
				//5.光照颜色：
				fixed4 c = fixed4(ambient + diffuseColor + specularColor , 1.0);
                return c;
            }
            ENDCG
        }
    }
	FallBack "Specular"
}